<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>统一用户授权管理系统</title>
<%@ include file="/include/header.jsp"%>
<%@ include file="/include/zTree.jsp"%>
<script type="text/javascript">
	function initTree () {
		var setting = {
			data : {
				simpleData : {
					enable : true
				// idKey:"id", 
				// pIdKey:"pId", 
				}
			}, check : {
				enable : true, chkboxType : {
					"Y" : "ps", "N" : "ps"
				}
			}, view : {
				showLine : false
			}, async : {
				enable : true, url : "${ctx}/sysAuthority/authorityTree", autoParam : [ "id" ], otherParam : {//这个是额外的附加的参数
					diyParams : $("#roleId").val()
				}, dataType : "json",//默认text 
				//type:"get",//默认post 
				dataFilter : filter
			//异步返回后经过Filter 
			}, callback : {
				asyncSuccess : zTreeOnAsyncSuccess,//异步加载成功的fun 
				asyncError : zTreeOnAsyncError, //加载错误的fun 
				beforeClick : beforeClick
			//捕获单击节点之前的事件回调函数 
			}
		};

		//treeId是authorityTree 
		function filter (treeId, parentNode, childNodes) {
			if (!childNodes) {
				return null;
			}
			for (var i = 0, l = childNodes.length; i < l; i++) {
				childNodes[i].name = childNodes[i].name.replace('', '');
			}
			return childNodes;
		}

		function beforeClick (treeId, treeNode) {
			if (!treeNode.isParent) {
				//alert("请选择父节点"); 
				return false;
			} else {
				return true;
			}
		}
		function zTreeOnAsyncError (event, treeId, treeNode) {
			alert("异步加载失败!");
		}
		function zTreeOnAsyncSuccess (event, treeId, treeNode, msg) {

		}
		$.fn.zTree.init($("#authorityTree"), setting);
	}

	$(function () {
		initTree();
		$("#saveBtn").click(function () {
			var treeObj = $.fn.zTree.getZTreeObj("authorityTree");
			var nodes = treeObj.getCheckedNodes(true);
			var ids = "";
			for (var i = 0; i < nodes.length; i++) {
				ids += nodes[i].id + ",";
			}
			$.post("${ctx}/sysRole/setAuthority", {
				authorityIds : ids, roleId : $("#roleId").val(), csrfToken : "${csrfToken}"
			}, function (result) {
				if ("succ" == result) {
					alert("操作成功");
					parent.replaceByUrl('${ctx}/sysRole/list');
					parent.layerClose(window.name);
				} else {
					alert(result);
				}
			}, "json");
		});
	});
</script>
</head>
<body style="background-color: white;">
	<div style="padding: 10px;">
		<div class="b_box">
			<div class="p_bar"></div>
			<div class="action_box">
				<span class="action_tit">选择角色：${sysRole.roleName}</span>
			</div>

			<div class="ch_panel " style="background-color: white; height: 300px;">
				<ul id="authorityTree" class="ztree" style="overflow: auto; height: 290px"></ul>
			</div>
			<div class="action_box m_t10 t_c">
				<input type="button" class="a_btn" value="保存" id="saveBtn" />
			</div>
			<input type="hidden" id="roleId" value="${sysRole.roleId}" />
		</div>
	</div>
</body>
</html>